#!/bin/sh

prefix=@prefix@
exec_prefix=@exec_prefix@
datarootdir=@datarootdir@

if [ -x @bindir@/showiframe ]; then
	if [ -f @sysconfdir@/enigma2/backdrop.mvi ]; then
		@bindir@/showiframe @sysconfdir@/enigma2/backdrop.mvi
	elif [ -f @sysconfdir@/enigma2/bootlogo.mvi ]; then
		@bindir@/showiframe @sysconfdir@/enigma2/bootlogo.mvi
	elif [ -f @datadir@/bootlogo.mvi ]; then
		@bindir@/showiframe @datadir@/bootlogo.mvi
	fi
fi

# hook to execute scripts always before enigma2 start
if [ -x @bindir@/enigma2_pre_start.sh ]; then
	@bindir@/enigma2_pre_start.sh
fi

if [ -d /home/root ]; then
	export HOME=/home/root
	cd
fi

LIBS=@libdir@/libopen.so.0.0.0

sync
LD_PRELOAD=$LIBS @bindir@/enigma2

# enigma2 exit codes:
#
# 1 - halt
# 2 - reboot
# 3 - restart enigma
#
# >128 signal

ret=$?
case $ret in
	1)
		/sbin/halt
		;;
	2)
		/sbin/reboot
		;;
	3)
		;;
	4)
		/sbin/rmmod lcd
		/usr/sbin/fpupgrade --upgrade 2>&1 | tee /home/root/fpupgrade.log
		sleep 1;
		/sbin/rmmod fp
		/sbin/modprobe fp
		/sbin/reboot
		;;
	5)
		if ! grep -q config.misc.RestartUI /etc/enigma2/settings; then
			echo "config.misc.RestartUI=true" >>/etc/enigma2/settings
		fi
		;;
	42)
		# bind the console (when available)
		[ -f /sys/class/vtconsole/vtcon1/bind ] && echo 1 > /sys/class/vtconsole/vtcon1/bind
		opkg upgrade 2>&1 | tee /home/root/ipkgupgrade.log
		/sbin/reboot
		;;
	43)
		#auto install and autobackup
		[ -f /sys/class/vtconsole/vtcon1/bind ] && echo 1 > /sys/class/vtconsole/vtcon1/bind
		/etc/init.d/settings-restore.sh
		/etc/init.d/avahi-daemon stop
		ifdown eth1
		ip addr flush dev eth1 scope global
		ifdown eth0
		ip addr flush dev eth0 scope global
		/etc/init.d/networking stop
		killall -9 udhcpc
		rm /var/run/udhcpc*
		/etc/init.d/networking start
		/etc/init.d/avahi-daemon start
		/etc/init.d/autoinstall.sh
		;;
	*)
		;;
esac
